<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文字消失</title>
    
</head>
<body>
    <script>
        var num = 1;
        function stary(){
            num++;
            if (num%2 == 0) {
                var div = document.querySelector('#btn')

                var newH2 = document.createElement('h2');
                var txt = document.createTextNode('输出文本');
                newH2.appendChild(txt);
                div.appendChild(newH2);
            }else if(num%2 != 0){
                document.body.innerHTML = " ";
            }
        }
    </script>
    <!-- 点击一次文字出现，再次点击文字消失 -->
    <div id="btn" style="text-align: center; margin:50px">
        <button onclick="stary()">触发按钮</button>
    </div>
</body>
</html>